//
//  IGListTableViewDemo.m
//  Demo
//
//  Created by lx on 2018/12/25.
//  Copyright © 2018 mgz. All rights reserved.
//

#import "IGListTableViewDemo.h"
#import "TextVM.h"
#import "TextAndImgVM.h"
#import "ModelVM.h"
#import <IGListKit.h>
#import <Masonry.h>
#import "Model.h"
#import "BModel.h"
#import <MGZTools.h>
@interface IGListTableViewDemo ()<IGListAdapterDelegate,IGListAdapterDataSource>
@property (nonatomic,strong)IGListCollectionView *collectionView;
@property (nonatomic,strong)IGListAdapter *adapter;
@property (nonatomic,strong)NSMutableArray *dataArr;//真正数据源
@end

@implementation IGListTableViewDemo

- (void)viewDidLoad {
    [super viewDidLoad];
    [self bindView];
    [MGZLogOutputWindow printLog:@"欢迎来到IGListDemo"];
    
    // Do any additional setup after loading the view.
}

- (NSArray<id<IGListDiffable>> *)objectsForListAdapter:(IGListAdapter *)listAdapter{
    
    return (NSMutableArray<id <IGListDiffable>> *)self.dataArr;
}


- (IGListSectionController *)listAdapter:(IGListAdapter *)listAdapter sectionControllerForObject:(id)object{
    
    if ([object isKindOfClass:[NSNumber class]]) {
        return [[TextAndImgVM alloc]init];
    }else if([object isKindOfClass:[Model class]] || [object isKindOfClass:[BModel class]]){
        return [[ModelVM alloc]init];
    }else{
        return [[TextVM alloc]init];
    }
}

- (nullable UIView *)emptyViewForListAdapter:(nonnull IGListAdapter *)listAdapter {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    view.backgroundColor = [UIColor blueColor];
    return view;
}



//将要显示secViewVC了.
- (void)listAdapter:(IGListAdapter *)listAdapter willDisplayObject:(id)object atIndex:(NSInteger)index{
    
}

//已经结束显示sectionVC了.
- (void)listAdapter:(IGListAdapter *)listAdapter didEndDisplayingObject:(id)object atIndex:(NSInteger)index{
    
    
}


- (void)bindView{
    self.title = @"IGListDemo";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(self.view);
    }];
    _collectionView.backgroundColor = [UIColor lightGrayColor];
}



- (IGListCollectionView *)collectionView{
    if (_collectionView == nil) {
        IGListCollectionViewLayout *layout = [[IGListCollectionViewLayout alloc]initWithStickyHeaders:NO scrollDirection:UICollectionViewScrollDirectionVertical topContentInset:0 stretchToEdge:NO];
        _collectionView=  [[IGListCollectionView alloc]initWithFrame:CGRectZero listCollectionViewLayout:layout];
        IGListAdapterUpdater *updater = [[IGListAdapterUpdater alloc]init];
        
        _adapter = [[IGListAdapter alloc]initWithUpdater:updater viewController:self workingRangeSize:0];
        _adapter.delegate = self;
        _adapter.dataSource = self;
        _adapter.collectionView = _collectionView;
    }
    return _collectionView;
}

- (NSMutableArray *)dataArr{
    if (_dataArr == nil) {
        
        //原生Model 遵循了<IGListDiffable>
        Model *model = [[Model alloc]init];
        model.str = @"这是第一个model";
        model.number = @(2222);
        
        //继承BaseIGListModel 简单方便r
        BModel *model1 = [[BModel alloc]init];
        model1.str = @"这是第一个Bmodel";
        model1.number = @(3333);
        _dataArr = [[NSMutableArray alloc]initWithArray:@[@"11111",@(2222),model1,@"333333333333333333333",@(3333),model]];
    }
    return _dataArr;
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

//- (void)encodeWithCoder:(nonnull NSCoder *)aCoder {
//
//}
//
//- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection {
//
//}
//
//- (void)preferredContentSizeDidChangeForChildContentContainer:(nonnull id<UIContentContainer>)container {
//
//}

//- (CGSize)sizeForChildContentContainer:(nonnull id<UIContentContainer>)container withParentContainerSize:(CGSize)parentSize {
//    return parentSize;
//}
//
//- (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(nonnull id<UIContentContainer>)container {
//
//}
//
//- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator {
//
//}
//
//- (void)willTransitionToTraitCollection:(nonnull UITraitCollection *)newCollection withTransitionCoordinator:(nonnull id<UIViewControllerTransitionCoordinator>)coordinator {
//
//}
//
//- (void)didUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context withAnimationCoordinator:(nonnull UIFocusAnimationCoordinator *)coordinator {
//
//}
//
//- (void)setNeedsFocusUpdate {
//
//}
//
//- (BOOL)shouldUpdateFocusInContext:(nonnull UIFocusUpdateContext *)context {
//    return YES;
//}
//
//- (void)updateFocusIfNeeded {
//
//}

@end
